package com.osfw.system.service.monitor.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osfw.framework.utils.DateUtils;
import com.osfw.framework.utils.StringUtils;
import com.osfw.framework.utils.text.Convert;
import com.osfw.system.entity.monitor.SysLogininfor;
import com.osfw.system.mapper.monitor.SysLogininforMapper;
import com.osfw.system.service.monitor.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 系统访问日志情况信息 服务层处理
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor> implements ISysLogininforService {

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        save(logininfor);
    }

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        LambdaQueryWrapper<SysLogininfor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(logininfor.getIpaddr()), SysLogininfor::getIpaddr, logininfor.getIpaddr());
        queryWrapper.eq(StringUtils.isNotEmpty(logininfor.getStatus()), SysLogininfor::getStatus, logininfor.getStatus());
        queryWrapper.like(StringUtils.isNotEmpty(logininfor.getLoginName()), SysLogininfor::getLoginName, logininfor.getLoginName());
        queryWrapper.ge(logininfor.getBeginTime() != null, SysLogininfor::getLoginTime, logininfor.getBeginTime());
        queryWrapper.le(logininfor.getEndTime() != null, SysLogininfor::getLoginTime, logininfor.getEndTime());
        return list(queryWrapper);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public boolean deleteLogininforByIds(String ids) {
        return removeByIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public boolean cleanLogininfor() {
        return remove(new LambdaQueryWrapper<SysLogininfor>());
    }
}
